package com.wlcorp.forcazamis.dao.pi.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.pi.IPidetailDAO;
import com.wlcorp.forcazamis.domain.pi.Pidetail;

public class PidetailDAOImpl extends HibernateDaoSupport implements IPidetailDAO{

	@SuppressWarnings("unchecked")
	public List getPidetailList(final Pidetail pidetail) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select pidetail from Pidetail as pidetail where pidetail.deleteFlag=false");
//			.append(" and pidetail.department.departmentId = ").append(pidetail.getDepartmentId());
		if(null != pidetail.getPiCode()){
			queryString.append(" and pidetail.piCode like '%").append(pidetail.getPiCode()).append("%'");
		}
		queryString.append(" order by pidetail.piDate desc");
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != pidetail && null != pidetail.getMinRownum()){
					query.setFirstResult(pidetail.getMinRownum());
					query.setMaxResults(pidetail.getNumPerPage());
				}
				return query.list();
			}
			
		});
	}

	public Long getPidetailCount(final Pidetail pidetail) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select count(pidetail) from Pidetail as pidetail where pidetail.deleteFlag=false");
		if(null != pidetail.getPiCode()){
			queryString.append(" and pidetail.piCode = '").append(pidetail.getPiCode()).append("'");
		}
		return (Long)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				if(null != pidetail && null != pidetail.getMinRownum()){
					query.setFirstResult(pidetail.getMinRownum());
					query.setMaxResults(pidetail.getNumPerPage());
				}
				return query.uniqueResult();
			}
			
		});
	}

	public Pidetail getPidetailById(Long pidetailId) throws DaoException {
		return (Pidetail)getHibernateTemplate().get(Pidetail.class,pidetailId);
	}

	public void deletePidetail(Pidetail pidetail) throws DaoException {
		pidetail.setDeleteFlag(true);
		getHibernateTemplate().update(pidetail);
	}

	public void savePidetail(Pidetail pidetail) throws DaoException {
		getHibernateTemplate().flush();
		getHibernateTemplate().clear();
		getHibernateTemplate().saveOrUpdate(pidetail);
		
	}

	public String getLatestPiCodeByCustomer(final String customerId) throws DaoException {
		final StringBuffer queryString = new StringBuffer("select pidetail.piCode from Pidetail as pidetail where 1=1 ")
			.append(" and pidetail.piid = (select max(pidetail1.piid) from Pidetail as pidetail1 where ")
			.append(" pidetail.customer.customerId = ").append(customerId).append(")");
		return (String)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(queryString.toString());
				return query.uniqueResult();
			}
			
		});
	}
	
}
